package com.drakulo.hi.states;

import java.awt.Point;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.dk.hi.core.helper.TextHelper;
import com.drakulo.hi.logic.battle.BattleAction;
import com.drakulo.hi.logic.battle.BattleField;
import com.drakulo.hi.logic.battle.BattleLine;
import com.drakulo.hi.logic.battle.BattleMoveAction;

/**
 * The battle state
 */
public class BattleState extends BasicGameState {
	public static final int VICTORY = 1;
	public static final int IN_PROGRESS = 0;
	public static final int DEFEAT = -1;

	/**
	 * The battle line
	 */
	BattleLine battleLine;

	/**
	 * The battleField
	 */
	BattleField battleField;

	/**
	 * The action selected by the player
	 */
	BattleAction playerSelectedAction;

	/**
	 * The battle field square's top left coordinates selected by the player for
	 * the next action
	 */
	Point target;

	/**
	 * The battle field square's top left coordinates the mouse is on
	 */
	Point mouseTarget;

	/**
	 * The currently running action
	 */
	private BattleAction actionRunning;

	/**
	 * True if the battle is done
	 */
	private boolean battleIsDone;

	/**
	 * The battle result
	 */
	private int finalBattleStatus;

	@Override
	public int getID() {
		return 0;
	}

	/**
	 * Gets the target square the mouse is on
	 * 
	 * @param input
	 * @return
	 */
	private Point getMouseTarget(Input input) {
		// TODO
		return null;
	}

	/**
	 * Gets the player action
	 * 
	 * @param input
	 *            - the game input
	 */
	private BattleAction getPlayerAction(Input input) {
		this.mouseTarget = getMouseTarget(input); // For graphic rendering
		this.playerSelectedAction = selectAction(input);
		if (this.playerSelectedAction == null) {
			// No action is currently selected so we handle player movement
			this.target = getSelectedTarget(input);
			if (this.target != null) {
				// The target is OK so we create a new move action
				BattleMoveAction moveAction = new BattleMoveAction();
				moveAction.setTarget(this.target);
				this.target = null;
				// The action is validated and ready to be executed
				this.actionRunning = moveAction;
			}
		} else {
			// An action was selected, the player has now to select a target
			this.target = getSelectedTarget(input);
			if (this.target != null) {
				// The target is OK so we update the selected action with the
				// target
				this.playerSelectedAction.setTarget(this.target);
				this.target = null;
				// The action is validated and ready to be executed
				this.actionRunning = this.playerSelectedAction;
			}
		}

		// Nothing happened during this game cycle, no action to do
		return null;
	}

	/**
	 * Gets the target square clicked on the battleField
	 * 
	 * @param input
	 *            - the game input
	 * @return the suqare selected
	 */
	private Point getSelectedTarget(Input input) {
		if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
			// Mouse left button clicked, where?
			Point p = getMouseTarget(input);
			if (targetIsPossible(p)) {
				// The target is possible
				return p;
			}
			// Impossible to select this target.
			return null;
		}
		return null;
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
		this.actionRunning = null;
		this.battleIsDone = false;
		this.finalBattleStatus = IN_PROGRESS;
		this.playerSelectedAction = null;
	}

	public void initializeNewBattle() {
		// TODO
		this.battleLine = new BattleLine(null);
		this.battleField = new BattleField();
		this.playerSelectedAction = null;
		this.target = null;
		this.mouseTarget = null;

	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics g) throws SlickException {
		// TODO
		g.setColor(Color.white);
		g.fillRect(0, 0, 800, 600);
		g.setColor(Color.black);

		g.drawRect(50, 50, 200, 200);
		String text = "Il était une fois un petit héros quis ramassait des fleurs dans la verte prairie. Un orage éclata et il se mit à pleuvoir";
		TextHelper.drawText(50, 50, 200, text);

	}

	/**
	 * Get the action selected by the player for the current Hero
	 * 
	 * @param input
	 *            - the game input
	 * @return the battle action without target
	 */
	private BattleAction selectAction(Input input) {
		// TODO
		return null;
	}

	/**
	 * Tests if the point is a possible target for the selected action and
	 * current Hero
	 * 
	 * @param p
	 *            - The square's top left coordinates
	 * @return true if the target is OK
	 */
	private boolean targetIsPossible(Point p) {
		// TODO
		return false;
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int arg2) throws SlickException {
		// if (battleIsDone) {
		// // The battle is done
		// if (finalBattleStatus == VICTORY) {
		// // The player won. Show the victory panel with XP, money and
		// // items gained
		// game.enterState(State.BATTLE_REWARD);
		// } else {
		// // The player lost the battle. Show a Game Over screen
		// game.enterState(State.GAME_OVER);
		// }
		// } else {
		// if (actionRunning != null) {
		// // An action is in progress
		// if (actionRunning instanceof BattleEndTurnAction) {
		// // The current action is the end of the actor's turn
		// // The battle line head actor go to the tail
		// battleLine.endTurn();
		// actionRunning = null;
		// } else {
		// // Let's update the action. While the action is not done,
		// // the battle logic cannot continue
		// actionRunning.update();
		// if (actionRunning.isDone()) {
		// // If the action is done, the battle logic can continue
		// actionRunning = null;
		// playerSelectedAction = null;
		// }
		// }
		// } else {
		// // No action is running, the game logic can go on
		// int battleStatus = battleLine.getBattleStatus();
		// if (battleStatus != IN_PROGRESS) {
		// // The battle is done.
		// battleIsDone = true;
		// finalBattleStatus = battleStatus;
		// } else {
		// // Who's turn?
		// BattleActor actor = battleLine.getHeadActor();
		// if (actor.isAI()) {
		// // The head actor is handled by the AI.
		// actionRunning = actor.findAction();
		//
		// // Reset the mouse target when AI
		// mouseTarget = null;
		// } else {
		// Input input = container.getInput();
		// // The head actor is handled by player. Which action
		// // want he to do?
		// actionRunning = getPlayerAction(input);
		// // While the player don't chose an action, the battle
		// // logic waits
		// }
		// }
		// }
		// }
	}
}
